﻿/*
   Copyright 2012 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Flower.Directory.Host.Default.Advices;
using Flower.Services;
using Spring.Aop.Framework;
using Spring.Aop.Support;
using AopAlliance.Aop;
using Spring.Aop;
using System.Collections;

namespace Flower.Directory.Host.Default
{
    public class DirectoryProxyProvider : IDirectoryProxyProvider
    {
        [Flags]
        public enum Advices
        {
            Logging = 1,
            ProcessorNotification = 2,
            ArgumentsValidation = 4,
            ExceptionShielding = 8,
            ProcessDispatcherNotifier = 16,
            All = Logging | ProcessorNotification | ArgumentsValidation | ExceptionShielding | ProcessDispatcherNotifier
        }

        private readonly IAdvisor[] _customAdvisors;

        public DirectoryProxyProvider()
        {
            _customAdvisors = null;
        }

        public DirectoryProxyProvider(IList customAdvisors)
        {
            _customAdvisors = customAdvisors.Cast<IAdvisor>().ToArray();
        }

        public IDirectory MakeProxy(IDirectory directory)
        {
            return MakeProxy(directory, Advices.All);
        }

        public IDirectory MakeProxy(IDirectory directory, Advices advices)
        {
            if (directory == null) throw new ArgumentNullException("directory");

            string directoryTypeName = directory.GetType().Name;

            var factory = new ProxyFactory(directory);

            if (advices.HasFlag(Advices.ArgumentsValidation))
            {
                factory.AddAdvisor
                (
                    new DefaultPointcutAdvisor
                    {
                        Advice = new ArgumentsValidation(),
                        Order = 10
                    }
                );
            }

            if (advices.HasFlag(Advices.Logging))
            {
                factory.AddAdvisor
                (
                    new DefaultPointcutAdvisor
                    {
                        Advice = new Default.Advices.Logging(directoryTypeName),
                        Order = 10
                    }
                );
            }

            if (advices.HasFlag(Advices.ProcessorNotification))
            {
                factory.AddAdvisor
                (
                    new DefaultPointcutAdvisor
                    {
                        Advice = new ProcessorNotifier(),
                        Order = 20
                    }
                );
            }

            if (advices.HasFlag(Advices.ProcessDispatcherNotifier))
            {
                factory.AddAdvisor
                (
                    new DefaultPointcutAdvisor
                    {
                        Advice = new ProcessDispatcherNotifier(),
                        Order = 30
                    }
                );
            }

            if (advices.HasFlag(Advices.ExceptionShielding))
            {
                factory.AddAdvisor
                (
                    new DefaultPointcutAdvisor
                    {
                        Advice = new ExceptionShield(),
                        Order = 10
                    }
                );
            }

            if (_customAdvisors != null)
            {
                foreach (IAdvisor customAdvisor in _customAdvisors)
                {
                    factory.AddAdvisor(customAdvisor);
                }
            }

            return (IDirectory)factory.GetProxy();
        }
    }
}
